package com.sunday.pinxinghui.model.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.sunday.common.base.BaseFragment;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.widgets.ptr.PtrClassicFrameLayout;
import com.sunday.common.widgets.ptr.PtrDefaultHandler;
import com.sunday.common.widgets.ptr.PtrFrameLayout;
import com.sunday.common.widgets.ptr.PtrHandler;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.adapter.ProductClassifyChildAdapter;
import com.sunday.pinxinghui.adapter.ProductClassifyParentAdapter;
import com.sunday.pinxinghui.entity.IndexCategory;

import com.sunday.pinxinghui.model.products.ProductsListActivity;

import com.sunday.pinxinghui.network.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/8/3.
 */

public class ClassifyFragment extends BaseFragment {


//    @Bind(R.id.layout_search)
//    LinearLayout layoutSearch;
    @Bind(R.id.product_category)
    ListView productCategory;
    @Bind(R.id.product_category_list)
    GridView productCategoryList;
    @Bind(R.id.linearlayout)
    LinearLayout layout;
    @Bind(R.id.refresh_layout)
    PtrClassicFrameLayout refreshLayout;

    private List<IndexCategory> parentList = new ArrayList<>();
    private List<IndexCategory> childList = new ArrayList<>();
    private ProductClassifyParentAdapter parentAdapter;
    private ProductClassifyChildAdapter childAdapter;
    private String parentName;
    private int parentId;
    private int selectIndex;

    public static ClassifyFragment newInstance() {
        ClassifyFragment fragment = new ClassifyFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.classify_fragment, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initAdapters();
        getParentCategory();
    }


//    @OnClick({R.id.layout_search})
//    void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.layout_search:
//                intent = new Intent(mContext, SearchProductListActivity.class);
//                startActivity(intent);
//                break;
//        }
//    }


    private void initAdapters() {
        refreshLayout.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getParentCategory();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                /*if (productCategory.isFocused()) {
                    return PtrDefaultHandler.checkContentCanBePulledDown(frame, productCategory, header);
                } else if (productCategoryList.isFocused()) {
                    return PtrDefaultHandler.checkContentCanBePulledDown(frame, productCategoryList, header);
                } else {
                    return false;
                }*/
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, productCategory, header)&&
                        PtrDefaultHandler.checkContentCanBePulledDown(frame, productCategoryList, header);
            }
        });
        parentAdapter = new ProductClassifyParentAdapter(mContext, parentList);
        productCategory.setAdapter(parentAdapter);
        childAdapter = new ProductClassifyChildAdapter(mContext, childList);
        productCategoryList.setAdapter(childAdapter);
        productCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                parentAdapter.setSelectPosition(position);
                selectIndex = position;
                parentId = parentList.get(position).getId();
                parentName = parentList.get(position).getName();
                if (parentList.get(position).getChildren() != null) {
                    childList.clear();
                    childList.addAll(parentList.get(position).getChildren());
                }
                parentAdapter.notifyDataSetChanged();
                childAdapter.notifyDataSetChanged();

            }
        });
        productCategoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                IndexCategory productBrand = (IndexCategory) parent.getAdapter().getItem(position);
                //intent = new Intent(mContext, ProductListActivity.class);
                intent = new Intent(mContext, ProductsListActivity.class);
                intent.putExtra("catId", productBrand.getId());
                intent.putExtra("parentId", productBrand.getParentId());
                intent.putExtra("parentName", parentName);
                startActivity(intent);
            }
        });
    }

    public  void refresh(){
        selectIndex=0;
        getParentCategory();
    }

    private void getParentCategory() {
        Call<ResultDO<List<IndexCategory>>> call = ApiClient.getApiAdapter().getCategory();
        call.enqueue(new Callback<ResultDO<List<IndexCategory>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<IndexCategory>>> call, Response<ResultDO<List<IndexCategory>>> response) {
                if (isFinish) {
                    return;
                }
                refreshLayout.refreshComplete();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    parentList.clear();
                    parentList.addAll(response.body().getResult());
                    if (parentList != null && parentList.size() > 0)
                        parentId = parentList.get(selectIndex).getId();
                    if (parentList.get(selectIndex).getChildren() != null) {
                        childList.clear();
                        childList.addAll(parentList.get(selectIndex).getChildren());
                        parentName = parentList.get(selectIndex).getName();

                    }
                    parentAdapter.setSelectPosition(selectIndex);
                    parentAdapter.notifyDataSetChanged();
                    childAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<IndexCategory>>> call, Throwable t) {
                refreshLayout.refreshComplete();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
